package com.tfswx.jcyw.ruleenginedesigner.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.tfswx.jcyw.App;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;

/**
 * @author Ricky
 * @date 2020/11/19
 */
@Configuration
@EnableKnife4j
@EnableSwagger2
public class SwaggerConfiguration {

    @Resource
    private App app;
    @Resource
    private BbinfoConfig bbinfoConfig;

    @Bean
    public Docket groupRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName(app.getAppName()).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        String title = app.getAppName();
        String description = "# " + title;
        String version = bbinfoConfig.getVersion();
        title += " V" + version;
        String time = bbinfoConfig.getFbsj();
        description += " 提交时间：" + time;
        apiInfoBuilder.version(version);
        return apiInfoBuilder.title(title).description(description).build();
    }
}
